package com.lc.sell.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lc.sell.entity.Merchant;
import com.lc.sell.mapper.MerchantMapper;
import com.lc.sell.service.MerchantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lc.sell.util.IdGenerator;
import com.lc.sell.util.TimeUtils;
import com.lc.sell.vo.GoodsVO;
import com.lc.sell.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙晨
 * @since 2022-02-01
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;


    @Override
    public ResultVo findAll() {
        List<GoodsVO> all = merchantMapper.findAll();
        return ResultVo.ok("添加成功",all);
    }

    @Override
    public ResultVo findByMerchant(Merchant merchant,String one,String two) {
        List<Merchant> list = merchantMapper.selectList(new QueryWrapper<Merchant>()
                .like(StringUtils.isNotBlank(merchant.getName()),"name", merchant.getName())
                .like(StringUtils.isNotBlank(merchant.getUseruuid()),"useruuid", merchant.getUseruuid())
                .like(StringUtils.isNotBlank(merchant.getPhone()),"phone", merchant.getPhone())
                .between(StringUtils.isNotBlank(one),"createTime",one,two)
        );
        return ResultVo.ok("查询成功",list);
    }

    @Override
    @Transactional
    public ResultVo addMerchant(Merchant merchant) {
        merchant.setPassword("888888");
        merchant.setCreateTime(TimeUtils.getNowDate());
        merchant.setUseruuid(IdGenerator.UidGenerator());
        int i = merchantMapper.insert(merchant);
        if (i>0){
            return ResultVo.ok("添加成功");
        }
        return ResultVo.fail("添加失败");
    }

    @Override
    @Transactional
    public ResultVo deleteMerchant(Integer id) {
        int i = merchantMapper.deleteById(id);
        if (i>0){
            return ResultVo.ok("删除成功");
        }
        return ResultVo.fail("删除失败");
    }

    @Override
    @Transactional
    public ResultVo updateMerchant(Merchant merchant) {
        int i=  merchantMapper.updateById(merchant);
        if (i>0){
            return ResultVo.ok("修改成功");
        }
        return ResultVo.fail("修改失败");
    }

    @Override
    public ResultVo findByMerchantId(Integer id) {
        Merchant merchant = merchantMapper.selectById(id);
        return  ResultVo.ok("查询成功",merchant);
    }

    @Override
    public ResultVo findAllMerchant(long current, long size) {
        Page<Merchant> selectPage = merchantMapper.selectPage(new Page<>(current,size), null);
        return ResultVo.ok("查询成功",selectPage);
    }

    @Override
    @Transactional
    public ResultVo showMerchant(Integer id) {
        Merchant merchant = merchantMapper.selectById(id);
        if (merchant.getIsShowBackstage().equals(0)){
            merchant.setIsShowBackstage(1);
        }else {
            merchant.setIsShowBackstage(0);
        }
        merchantMapper.updateById(merchant);
        return ResultVo.ok("显示/隐藏");
    }

    @Override
    public ResultVo toLogin(HttpServletRequest request,String username, String password) {
        Merchant merchant = merchantMapper.selectOne(new QueryWrapper<Merchant>().lambda().eq(Merchant::getUseruuid, username).eq(Merchant::getPassword, password));
        if (merchant == null){
            return ResultVo.fail("用户名或密码错误!");
        }
        request.getSession().setAttribute("mid",merchant.getId());
        request.getSession().setAttribute("mname",merchant.getRealname());
        return ResultVo.ok("登录成功!");
    }


}
